package objects.test;

import objects.base.Collidable;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.state.StateBasedGame;
import util.level.Room;

/**
 * A class defining moving platforms that bounce
 * off of walls.
 * 
 * @author John
 */

public class BounceBlock extends Collidable
{	
	/**
	 * Initializes a bouncing block of given size at the given position
	 * with the given speed.
	 * 
	 * @param initX - The initial X position.
	 * @param initY - The initial Y position.
	 * @param initWidth - The initial width of the object.
	 * @param initHeight - The initial height of the object.
	 * @param initXSpeed - The initial X speed to give the object.
	 * @param initYSpeed - The initial X speed to give the object.
	 */
	public BounceBlock(float initX, float initY, float initWidth, float initHeight, float initXSpeed, float initYSpeed)
	{
		// Call the super constructor
		super(initX, initY, initWidth, initHeight);
		// Set the initial speed
		xMotion[0].speed = initXSpeed;
		yMotion[0].speed = initYSpeed;
		// Increase the priority to push things out of the way
		pushPriority = 3;
		// Do not let moving blocks be affected by anything else
		hasInertia = false;
	}

	/**
	 * Initializes a bouncing block of given size at the given position.
	 * 
	 * @param initX - The initial X position.
	 * @param initY - The initial Y position.
	 * @param initWidth - The initial width of the object.
	 * @param initHeight - The initial height of the object.
	 * @param initSpeed - The initial speed factor to give the object.
	 */
	public BounceBlock(float initX, float initY, float initWidth, float initHeight, float initSpeed)
	{
		// Call the constructor with random speeds based on the speed factor
		this(initX, initY, initWidth, initHeight,
			(float) (((Math.random()*9)+3)*initSpeed*(Math.random()<0.5?-1:1)),
			(float) (((Math.random()*9)+3)*initSpeed*(Math.random()<0.5?-1:1)));
	}
	
	/**
	 * Makes the block bounce off the sides of the screen.
	 * 
	 * @param gc {@inheritDoc}
	 * @param game {@inheritDoc}
	 */
	public void step(GameContainer gc,StateBasedGame sbg,Room room)
	{
		// Do movement
		super.step(gc,sbg,room);
		
		// If you hit the side walls, bounce off horizontally
		if (horizStop[0] || horizStop[1])
		{
			xMotion[0].speed = -xMotion[0].speed;
		}
		// Otherwise, bounce off vertically
		if (vertStop[0] || vertStop[1])
		{
			yMotion[0].speed = -yMotion[0].speed;
		}
	}
}
